const fs = require('fs')
const path = require('path')
const http = require('http')
const server = http.createServer()
const qs = require('querystring')


server.on('request', (req, res) => {
  if (req.url == '/api/province') {
    fs.readFile(path.join(__dirname, './12312/province.json'), (err, data) => {
      res.end(data.toString())
    })
  } else if (req.url.split('?')[0] == '/api/city') {
    // const { pname } = qs.parse(req.url.split('?')[1])
    // console.log(req.url.split('?')[1]);
    const p = req.url.split('?')[1]
    // console.log(p);
    const { pname } = qs.parse(p)
    console.log(pname);
    fs.readFile(path.join(__dirname, './12312/city.json'), (err2, data2) => {
      // console.log(data2.toString())
      // console.log(JSON.parse(data2.toString())[pname])
      // res.end(JSON.stringify(JSON.parse(data2.toString())[pname]))
      // console.log(JSON.parse(data2.toString()));
      res.end(JSON.stringify(JSON.parse(data2.toString())[pname]))
    })
  }
})
server.listen(3000, '192.168.87.34', () => {
  console.log('我监听到了哦');
})